package pathfind;

import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;

import javax.swing.JFrame;

public class PathFindTester extends Component{
	
	private static final int scale=20;
	NodeMap map = new NodeMap(21,20);
	PathFinder finder = new PathFinder(map);
	Path path;
public PathFindTester()
{
	addMouseListener(new clickact(this));
	addMouseMotionListener(new moveact(this));
}
public static void main(String args[])
{
	EventQueue.invokeLater(new Runnable() {

		public void run() {
			JFrame frame = new JFrame();
			frame.setTitle("Path finder tester");
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setSize(20*scale+20, 20*scale);
			frame.setResizable(false);
			frame.setVisible(true);
			
			PathFindTester tester = new PathFindTester();
			frame.add(tester);
			
		}
	});
}
public void run()
{
	
}
public void paint(Graphics g){
	g.setColor(Color.GREEN);
	if(path != null){
	{
		if(path.isSolution())
		for(Point i:path.getList())
		{
			g.fillRect(i.getx()*scale, i.gety()*scale, scale, scale);
		}
	}}
	g.setColor(Color.GRAY);
	
	for(int y = 0;y<map.getsizeY();y++)
	{
		for(int x = 0;x<map.getsizeX();x++)
		{
			if(map.isblock(x, y))
			{g.fillRect(x*scale, y*scale, scale, scale);}
			g.drawString(""+map.getNode(x, y), x*(scale)+5, y*(scale)+15);
		}
	}
	
}
public class moveact implements MouseMotionListener
{
	PathFindTester t;
	public moveact(PathFindTester t){
		this.t = t;
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		int x=e.getX()/t.scale,y=e.getY()/t.scale;
		if(e.isShiftDown())
		{
			t.map.setblock(x, y, -1);
			t.repaint();
		}
		if(e.isControlDown())
		{
			t.map.setblock(x, y, 0);
			t.repaint();
		}
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		
	}
	
}
public class clickact implements MouseListener
{
	PathFindTester t;
public clickact(PathFindTester t)
{
	this.t =t;
}
	@Override
	public void mouseClicked(MouseEvent e) {
		int x=e.getX()/t.scale,y=e.getY()/t.scale;
		if(e.getButton() == 3 && !t.map.isblock(x,y) && t.map.getstart() != null){
		t.path = new Path(t.map,x,y);
		t.repaint();
		}
		if(e.getButton() == 1 && !t.map.isblock(x,y))
		{
			t.finder.start(e.getX()/t.scale,e.getY()/t.scale);
			t.finder.run();
			t.path = new Path(t.map,t.path.get(0).getx(),t.path.get(0).gety());
			t.repaint();
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
}
}
